package top.hidoctor.auscultation.feature.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.util.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.GongzuotaiBzAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.LableBean;
import top.hidoctor.auscultation.event.WorkYbEvent;
import top.hidoctor.auscultation.net.c.LabelDetailContract;
import top.hidoctor.auscultation.net.p.LabelDetailPresenter;

public class WorkCenterBzActivity extends BaseActivity implements LabelDetailContract.View {

    private RecyclerView recycler;
    private GongzuotaiBzAdapter adapter;
    private Button btnFinish;
    private String type;
    private LabelDetailPresenter presenter;
    private LableBean info;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_center);
        EventBus.getDefault().register(this);
        presenter = new LabelDetailPresenter(this, this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        String name = intent.getStringExtra("name");
        presenter.getDetail(id, true);

        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tvTitle.setText(name);

        btnFinish = findViewById(R.id.btn_finish);
        recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new GongzuotaiBzAdapter(R.layout.item_shenhe_center, null));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                LableBean.SampleBean bean = (LableBean.SampleBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.btn_shenhe:
                        Intent intent = new Intent(WorkCenterBzActivity.this, LabelDetailActivity.class);
                        intent.putExtra("sampleId", bean.getId());
                        intent.putExtra("info", bean);
                        intent.putExtra("labelId", info.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (info.getSample() == null || info.getSample().size() == 0) {
                    showToast("暂无标注样本");
                    finish();
                } else {
                    boolean isFinish = true;
                    List<LableBean.SampleBean> sample = info.getSample();
                    for (int i = 0; i < sample.size(); i++) {
                        if (sample.get(i).getLabelStatus() != 20) {
                            isFinish = false;
                            break;
                        }
                    }

                    if (isFinish) {
                        if (info.getStatus() == 20) {
                            showToast("样本已完成标注");
                        } else {
                            presenter.updateLabel(info.getId());
                        }
                    } else {
                        showToast("您还有暂未标注的样本");
                    }
                }

            }
        });
    }

    @Override
    public void getDetailSuc(LableBean bean) {
        if (bean != null) {
            this.info = bean;
            adapter.setNewData(bean.getSample());
        }
    }

    @Override
    public void updateLabelSuc() {
        finish();
        EventBus.getDefault().post(new WorkYbEvent());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(WorkYbEvent event) {
        presenter.getDetail(id, true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
